<?php
/**
 * 功 能：用户修改登录密码
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/6
 * Time: 16:34
 */
class ChangepasswordController extends Yaf_Controller_Abstract
{
    function IndexAction()
    {
        $request = $this->getRequest();
        $user_number = $request->getPost("user_number",""); // 用户名
        $yiyuan_id = $request->getPost("yiyuan_id",""); // 医院编号
        $old_password = $request->getPost("old_password",""); // 原密码
        $new_password = $request->getPost("new_password",""); // 新密码
        $re_new_password = $request->getPost("re_new_password",""); // 确认新密码
        $yiyuan_user = new YizhuHistoryModel();
        $statr = array();
        if($user_number == "")
        {
            Utility::apiJsonError(20026, '用户名不能为空');
        }
        if(empty($old_password) || empty($new_password) || empty($re_new_password) ){// 如果原密码为空
            Utility::apiJsonError(20006, '密码为空');
        }else{// 原密码不为空
            if($new_password != $re_new_password){// 新密码与确认新密码不同
                Utility::apiJsonError(20007, '两次新密码输入不相同');
            }else{
                if($new_password != $old_password){// 新密码与原密码不同
                    if(Yaconf::get("Yaconf::get.open_password") == "true"){
                        $old_password = md5($old_password);
                        $new_password = md5($new_password);
                    }
                    $user_info = DB::select("id")
                        ->from("yiyuan_user")
                        ->where("login_password","=",$old_password)
                        ->and_where("user_number","=",$user_number)
                        ->and_where("yiyuan_id","=",$yiyuan_id)
                        ->execute()
                        ->as_array();
                    if(is_array($user_info)&&count($user_info)>0){// 根据用户名和登录密码查询，用户id存在
                        foreach ($user_info as $item_userinfo)
                        {
                            $query = DB::update('yiyuan_user')
                                ->set(array('login_password' => $new_password))
                                ->where('id', '=', $item_userinfo['id']);
                            $query->execute();
                        }
                        $statr['state'] = 'ok';
                        $statr['error'] = 200;
                        $statr['msg'] = "";
                        Utility::apiJsonResultExtends($statr);
                    }else{// 用户id不存在
                        Utility::apiJsonError(20009, '旧密码输入错误');
                    }

                }else{ // 新密码与原密码相同
                    Utility::apiJsonError(20008, '新密码与旧密码不能相同');
                }
            }

        }
        //$async_result_json = json_encode($statr);

        // 压缩数据
        //$async_result_json = $this->compress($async_result_json,$_REQUEST["compression_http_data"]);
        //结束
        //$this->apiLog(strlen($check_result_json));
    }

}